package com.ghyf.mplay.bean;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.ghyf.mplay.datatype.AlignMode;
import com.ghyf.mplay.datatype.BackParam;
import com.ghyf.mplay.datatype.ConvertItem;
import com.ghyf.mplay.datatype.FontParam;
import com.ghyf.mplay.datatype.GradientColorType;
import com.ghyf.mplay.datatype.POS;
import com.ghyf.mplay.datatype.RGBA;
import com.ghyf.mplay.datatype.ShowLang;
import com.ghyf.mplay.datatype.ShowType;
import com.ghyf.mplay.view.MCountDown;

/**
 * 倒计时模块的信息包装类
 * @author 陈小红
 *
 */
public class CountDownInfo extends BaseModuleInfo {

	/*
	 * 文件版本 File_Version CHAR 1 模块名称 Module_Name CHAR CountDown1 UID Module_UID
	 * INT GID Module_GIU INT 模块位置 Mudule_Pos POS - 背景参数 Background BACK_PARAM
	 * 见§3.1.4节 - 对齐方式 Align ENUM 5 左边距 Left_Margin INT 0 上边距 Top_Margin INT 0
	 * 显示语言 Show_Lang ENUM 中文/英文/中英文 1 显示秒钟 Show_Second BOOL
	 * 如显示秒，则显示“XX分XX秒”或“XXMin.XXSec.”； 如不显示秒，则显示“XX分钟”或“XXMin.” 0 中英文切换时间
	 * Show_Interval INT 仅当显示语言为“中英文”时有效 5 时间字体 Time_Font FONT_PARAM 见§3.1.2节
	 * 宋体40号 提示信息字体 Tip_Font FONT_PARAM 见§3.1.2节 宋体20号 转义 Convert_Param - 转义条件
	 * Time INT 转义内容 Content CHAR 转义字体 Font FONT_PARAM 见§3.1.2节 宋体40号 文字显示选项
	 * Text_Show_Param ENUM 不处理/压缩显示/向左滚动 预留参数 0
	 */

	/**
	 * 得到根据BaseModule子类的信息初始化后的控件
	 * @param context 上下文
	 * @return	初始化后的控件
	 */
	@Override
	public MCountDown getView(Context context) {
		mContext=context;
		mCountDown = new MCountDown(context);
		//设置控件基本属性
		mCountDown.setModule_type(module_type);
		mCountDown.setzOrder(zOrder);
		mCountDown.setFile_version(file_version);
		mCountDown.setModule_name(module_name);
		mCountDown.setModule_uid(module_uid);
		mCountDown.setModule_gid(module_gid);
		
		mCountDown.setmCountDown(mCountDown); // 将控件的引用传递过去，便于设置显示文本。
		mCountDown.setShow_lang(show_lang);
		mCountDown.setShow_second(show_second);
		mCountDown.setShow_interval(show_interval);
		mCountDown.setTip_font(tip_font);
		mCountDown.setTime_font(time_font);
		mCountDown.setConvertItem_list(convertItem_list);
		mCountDown.setConvert_font(convert_font);

		// 控件位置及大小设置。
		initPosition(mCountDown, pos);

		// 背景设置
		initBackground(mCountDown, backgroud);

		
		//对齐方式实现九宫格中左上，左下，右上，右下对齐方式。
		setGravity();

		// 设置左、上的padding（用left、top margin变量设置）
		mCountDown.setPadding(left_margin, top_margin, 0, 0);

		return mCountDown;
	}
	
	/**
	 * 对齐方式实现九宫格中左上，左下，右上，右下对齐方式<br>
	 * 2014-4-30 下午2:23:54
	 *
	 */
	void setGravity(){
		switch (align) {
		case LOWLEFT:
			mCountDown.setGravity(Gravity.LEFT|Gravity.BOTTOM);
			break;
		case LOWMIDDLE:
			mCountDown.setGravity(Gravity.CENTER|Gravity.BOTTOM);
			break;
		case LOWRIGHT:
			mCountDown.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
			break;
		case MIDDLELEFT:
			mCountDown.setGravity(Gravity.LEFT|Gravity.CENTER);
			break;
		case CENTER:
			mCountDown.setGravity(Gravity.CENTER);
			break;
		case MIDDLERIGHT:
			mCountDown.setGravity(Gravity.RIGHT|Gravity.CENTER);
			break;
		case UPLEFT:
			mCountDown.setGravity(Gravity.LEFT|Gravity.TOP);
			break;
		case UPMIDDLE:
			mCountDown.setGravity(Gravity.CENTER|Gravity.TOP);
			break;
		case UPRIGHT:
			mCountDown.setGravity(Gravity.RIGHT|Gravity.TOP);
			break;
		default:
			break;
		}
	}
	/**
	 * 设置当前View显示/隐藏
	 * 2014-5-21 下午3:25:01
	 * @param isShow
	 *
	 */
	public void setVisibility(boolean isShow){
		if(isShow){
			mCountDown.setVisibility(View.VISIBLE);
		}else{
			mCountDown.setVisibility(View.GONE);
		}
	}
	/**
	 * @return the mCountDown
	 */
	public MCountDown getmCountDown() {
		return mCountDown;
	}

	/**
	 * @param mCountDown the mCountDown to set
	 */
	public void setmCountDown(MCountDown mCountDown) {
		this.mCountDown = mCountDown;
	}

	/**
	 * 初始化控件的背景
	 * 
	 * @param view
	 *            被设置的控件
	 * @param bg
	 *            控件的背景
	 */
	void initBackground(View view, BackParam bg) {
		switch (bg.getType()) {
		case NOTSHOW: // 背景不显示
			break;
		case GRADIENTCOLOR: // 背景类型为渐变色
			if (bg.getColorType() == GradientColorType.HORIZONTALGRADIENT) {
				//水平渐变 参考：url:http://blog.csdn.net/a_large_swan/article/details/7107126
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,new int[]{startColor,endColor});
			
		//		view.setBackgroundDrawable(gradientDrawable);
				//在android minSdkVersion=16下使用 之前的采用上面方法
				view.setBackgroundDrawable(gradientDrawable);
			} else {
				// 垂直渐变
				int startColor = Color.argb(bg.getGradientcolor1().getAlpha(), bg.getGradientcolor1().getRed(), bg.getGradientcolor1().getGreen(),
						bg.getGradientcolor1().getBlue());
				int endColor = Color.argb(bg.getGradientcolor2().getAlpha(), bg.getGradientcolor2().getRed(), bg.getGradientcolor2().getGreen(),
						bg.getGradientcolor2().getBlue());
				GradientDrawable gradientDrawable=new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{startColor,endColor});
				view.setBackgroundDrawable(gradientDrawable);
			}
			break;
		case PICTURE: // 背景类型为图片
			
			// View设置背景图片出现警告需要解决
			// 本地图片文件路径生成drawable
			Bitmap bitmap = BitmapFactory.decodeFile(bg.getBkfile().getPath());
			Drawable drawable = new BitmapDrawable(mContext.getResources(),bitmap);
			// 设置图片背景
			view.setBackgroundDrawable(drawable);
			
			
			break;
		case PURECOLOR: // 背景类型为纯色
			RGBA purecolor = bg.getPurecolor();
			view.setBackgroundColor(Color.argb(purecolor.getAlpha(),
					purecolor.getRed(), purecolor.getGreen(),
					purecolor.getBlue()));
			break;
		default: // 其他
			break;
		}
	}

	/**
	 * 初始化控件的位置
	 * 
	 * @param view
	 *            被设置的控件
	 * @param pos
	 *            控件的位置
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	void initPosition(View view, POS pos) {
		view.setX(pos.getLeft());
		view.setY(pos.getTop());
		view.setLayoutParams(new LayoutParams(pos.getWidth(), pos.getHeight()));
	}

	// <Backgroud Type="3" PureColor="(0,0,0,255)" GradientColor1="(0,0,0,255)"
	// GradientColor2="(0,0,0,255)" BKFile="" GradientColorType="0"/>
	private BackParam backgroud;

	private Context mContext=null;
	// <Align>5</Align>
	private AlignMode align;// 对齐方式

	// <Left_Margin>0</Left_Margin>
	private int left_margin;// 左边距

	// <Top_Margin>0</Top_Margin>
	private int top_margin;// 上边距

	// <Show_Lang>1</Show_Lang>
	private ShowLang show_lang;// 显示语言

	// <Show_Type>0</Show_Type> //显示方式 eg.左滚，上滚，静止，压缩
	private ShowType show_type;

	// <Show_Second>0</Show_Second>
	private boolean show_second; // 是否显示秒

	// <Show_Interval>5</Show_Interval>
	private int show_interval;// 中英文切换时间

	// <Time_Font Name="宋体" Size="70" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(255,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	private FontParam time_font;

	// <Tip_Font Name="宋体" Size="30" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	private FontParam tip_font;

	// <Convert_Param>
	// <Item Time="60" Content="列车即将到达" />
	// <Item Time="30" Content="列车进站" />
	// <Font Name="宋体" Size="45" Width="0" Escapement="0" Orientation="0"
	// Kerning="0" Spacing="0" Bold="0" Italic="0" Underline="0"
	// FaceColor="(0,0,0,255)" EdgeWidth="0" EdgeColor="(0,0,0,255)"
	// ShadowWidth="0" ShadowColor="(0,0,0,255)" ShadowAngle="0" />
	// </Convert_Param>

	private ArrayList<ConvertItem> convertItem_list;// 即将到达转义项集合

	private FontParam convert_font;// 转义字体

	private MCountDown mCountDown;

	public BackParam getBackgroud() {
		return backgroud;
	}

	public void setBackgroud(BackParam backgroud) {
		this.backgroud = backgroud;
	}

	public AlignMode getAlign() {
		return align;
	}

	public void setAlign(AlignMode align) {
		this.align = align;
	}

	public int getLeft_margin() {
		return left_margin;
	}

	public void setLeft_margin(int left_margin) {
		this.left_margin = left_margin;
	}

	public int getTop_margin() {
		return top_margin;
	}

	public void setTop_margin(int top_margin) {
		this.top_margin = top_margin;
	}

	public ShowLang getShow_lang() {
		return show_lang;
	}

	public void setShow_lang(ShowLang show_lang) {
		this.show_lang = show_lang;
	}

	public ShowType getShow_type() {
		return show_type;
	}

	public void setShow_type(ShowType show_type) {
		this.show_type = show_type;
	}

	public boolean isShow_second() {
		return show_second;
	}

	public void setShow_second(boolean show_second) {
		this.show_second = show_second;
	}

	public int getShow_interval() {
		return show_interval;
	}

	public void setShow_interval(int show_interval) {
		this.show_interval = show_interval;
	}

	public FontParam getTime_font() {
		return time_font;
	}

	public void setTime_font(FontParam time_font) {
		this.time_font = time_font;
	}

	public FontParam getTip_font() {
		return tip_font;
	}

	public void setTip_font(FontParam tip_font) {
		this.tip_font = tip_font;
	}

	public FontParam getConvert_font() {
		return convert_font;
	}

	public void setConvert_font(FontParam convert_font) {
		this.convert_font = convert_font;
	}

	public ArrayList<ConvertItem> getConvertItem_list() {
		return convertItem_list;
	}

	public void setConvertItem_list(ArrayList<ConvertItem> convertItem_list) {
		this.convertItem_list = convertItem_list;
	}


}